package com.athuahua.spzx.service.impl;

import com.athuahua.spzx.entity.Brand;
import com.athuahua.spzx.entity.vo.CategoryBrandVo;
import com.athuahua.spzx.dto.product.CategoryBrandDto;
import com.athuahua.spzx.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.CategoryBrand;
import com.athuahua.spzx.service.CategoryBrandService;
import com.athuahua.spzx.mapper.CategoryBrandMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huahua
 * @description 针对表【category_brand(分类品牌)】的数据库操作Service实现
 * @createDate 2024-07-31 14:14:40
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
        implements CategoryBrandService {

    @Resource
    private CategoryBrandMapper categoryBrandMapper;

    @Resource
    private BrandService brandService;

    @Override
    public IPage<CategoryBrandVo> pageInfo(Integer page, Integer limit, CategoryBrandDto categoryBrandDto) {
        IPage<CategoryBrandVo> iPage = new Page<>(
                page,
                limit
        );
        IPage<CategoryBrandVo> Ipage = categoryBrandMapper.IpageInfo(iPage, categoryBrandDto);
        return Ipage;
    }

    @Override
    public List<Brand> findBrandByCategoryId(Integer categoryId) {
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId, categoryId);
        List<CategoryBrand> categoryBrands = categoryBrandMapper.selectList(queryWrapper);
        if (!categoryBrands.isEmpty()) {
            List<Long> brandIds = categoryBrands.stream().map(CategoryBrand::getBrandId).distinct().toList();
            return brandService.listByIds(brandIds);
        }
        return null;
    }
}




